<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session;
use Config;
use App\Http\Service\User\UserService;

use Illuminate\Support\Facades\Redis;


class IndexController extends Controller
{


    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 首页
     */
    public function index()
    {
        return view("Index.useman");
    }

    public function roleSet()
    {
        return view("Index.operation_log");
    }


    public function getSmsCode(Request $request)
    {

    }


    /**
     * @return bool|mixed|string
     * 获取用户信息
     */
    public function getUserInfo()
    {
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        $user = new UserService();
        $data =$user->getUserInfo($where);
        return $data;
    }

    /**
     * @return bool|mixed|string
     * 流水号
     */
    public function getSerialNumber()
    {
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        $user = new UserService();
        $data =$user->getSerialNumber($where);
        return $data;
    }


    /**
     * @param Request $request
     * 快捷创建测试账户
     */

    public function QuickOpenTestAccount(Request $request)
    {
        $set_meal_id =$request->input("set_meal_type");

        $where = [];

        $where['username'] = self::randUserName(5);

        $user = new UserService();
        $ret =$user->checkUserName($where);

        if($ret['data']['tag'] != 'success')
        {
            self::QuickOpenTestAccount();
        }

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        $where['concent_num'] = 1;
        $where['account_type'] = '测试';
        $where['password'] = 1;
        $where['type'] = 1;
        $where['order_id'] = self::createUuid();
        $where['set_meal_type'] = $set_meal_id;

        $data = $user->addUserAccount($where);

        return $data;
    }


    /**
     * @param Request $request
     * @return bool|mixed|string
     * 快速开通付费账号
     */
    public function QuickOpenPayAccount(Request $request)
    {
        $where = [];
        $where = $request->except('_token');



        $where['username'] = self::randUserName(5);

        $user = new UserService();
        $ret =$user->checkUserName($where);

        if($ret['data']['tag'] != 'success')
        {
            self::QuickOpenTestAccount();
        }
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));

        $where['account_type'] = '付费';
        $where['password'] = self::randUserName(3);
        $where['order_id'] = self::createUuid();
        $where['type'] = 1;
        $where['concent_num'] = 1;
        $where['date'] = 1;
        $data = $user->addUserAccount($where);
        return $data;

    }

    /**
     * @param Request $request
     * 账户续费
     */
    public function renewUserAccount(Request $request)
    {
        $where = [];

        $where = $request->except('_token');
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        $where['order_id'] = self::createUuid();

        if( $where['renew_date'] < 0 || !$where['renew_date'])
        {
            $where['renew_date'] = 1;
        }

        $user = new UserService();
        $data =$user->renewUserAccount($where);

        return $data;
    }



    /**
     * @param Request $request
     * 获取用户账号列表
     */
    public function getUserAccountListData(Request $request)
    {
        $where = [];

        $where['start_date'] = '';
        $where['end_date'] = '';
        $where['username'] = '';
        $where['sort'] = '';
        $where['order_by'] = '';

        $where = $request->except('_token');

        foreach($where as $key=>$value)
        {
            if($value =='' || $value == 'undefined')
            {
                unset($where[$key]);
            }
        }

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        $user = new UserService();
//        dd($where);
        $data = $user->getUserAccountList($where);

        return $data;

    }


    /**
     * @param Request $request
     * 添加账号
     */
    public function addUserAccount(Request $request)
    {
        $where = [];
        $where= $request->except('_token');
        $where['token']= Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        $where['concent_num'] = ltrim($where['concent_num'],'0');


        if($where['account_type']== '测试')
        {
            unset($where['date']);
            unset($where['date_type']);
            unset($where['account']);
        }else{
            $where['price'] = $where['concent_num'] * $where['date'] * $where['account'];
        }

        $where['order_id'] = self::createUuid();


        $user = new UserService();

        $data = $user->addUserAccount($where);

        return $data;

    }

    /**
     * @param Request $request
     * 修改账户信息
     */
    public function updateUserAccount(Request $request)
    {
        $where = [];
        $where= $request->except('_token');
        $reg = "/[[:punct:]]/i";
        $where['remark'] = preg_replace($reg, '', $where['remark']);
        $where['token']= Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        $user = new UserService();
        $data = $user->updateUserAccount($where);

        return $data;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 删除账户/批量删除
     */
    public function deleteUserAccount(Request $request)
    {
        $where = [];

        $where['user_id']=trim($request->input('user_id'),',');

        if($request->has('remark'))
        {
            $where['remark']= $request->input('remark');
        }

        $where['token']=Session::get(Config::get('custom.setting.MEMBER_TOKEN'));

        $user = new UserService();
        $data = $user->deleteUserAccount($where);

        return $data;

    }


    /**
     * @param $random_length
     * @return string
     * 生成用户名
     */
    public function randUserName($random_length)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz";
        $random_string = '';
        for ($i = 0; $i < $random_length; $i++) {
            $random_string .= $chars [mt_rand(0, strlen($chars) - 1)];
        }
        return $random_string;
    }


    //可以指定前缀   生成用户ID
    static function createUuid($prefix = "")
    {
        $str = md5(uniqid(mt_rand(), true));
        $uuid  = substr($str,0,8) . '-';
        $uuid .= substr($str,8,4) . '-';
        $uuid .= substr($str,12,4) . '-';
        $uuid .= substr($str,16,4) . '-';
        $uuid .= substr($str,20,12);
        return $prefix . $uuid;
    }



    



}
